using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for MasterDetail
/// </summary>
public class MasterDetail
{
    HDDatabase HDD = new HDDatabase();
	public MasterDetail()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    private void getValueTable(string ControlId, DataRow drPageKey, DropDownList DDL)
    {
        string sSql = "select object_table from sy_obj where object_id='" + drPageKey["master_obj"].ToString() + "'";
        string sTableName = HDD.GetColumnValue(sSql).Replace("'", "''");
        sSql = "select * from [" + sTableName + "] "; 
        security objSec = new security();
        string sRowLevel = objSec.getRowLevelSecurityData(sTableName, sTableName);
        if (sRowLevel != "")
            sSql = sSql + " where 1 = 1 and " + sRowLevel;
        sSql += " order by [" + sTableName + "].[" + drPageKey["display_att"].ToString().Replace("'", "''") + "]";
        DataTable dtValues = HDD.GetDataTable(sSql);
        DDL.DataSource = dtValues;
        DDL.DataTextField = drPageKey["display_att"].ToString();
        DDL.DataValueField = "af_row_id";
        DDL.DataBind();

        /*foreach (DataRow dr in dtValues.Rows)
            DDL.Items.Add(new ListItem(dr[drPageKey["display_att"].ToString()].ToString(), dr["af_row_id"].ToString()));
        */
        dtValues.Dispose();
        dtValues = null;
    }

    public bool FillMasterValues(string ControlId, DataRow drPageKey, DropDownList DDL, string DefaultValue)
    {
        try
        {
            getValueTable(ControlId, drPageKey, DDL);
            if (HttpContext.Current.Request.QueryString["MK"] != null)
                if (HttpContext.Current.Request.QueryString["MK"] != "")
                    DDL.SelectedValue = HttpContext.Current.Request.QueryString["MK"];
            /*if (HttpContext.Current.Session["PK"] != null)
                if (HttpContext.Current.Session["PK"].ToString() != "")
                {
                    DDL.SelectedValue = HttpContext.Current.Session["PK"].ToString();
                }*/
            if (HttpContext.Current.Request.QueryString["VMODE"] != null)
                if (HttpContext.Current.Request.QueryString["VMODE"] == "1")
                    DDL.Enabled = false;
        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("FillMasterValues : Error : " + ex.Message);
            return false;
        }
        return true;
    }

    public bool FillMasterValues(string ControlId, DataRow drPageKey, TextBox txtobj,HiddenField hfobj, string DefaultValue)
    {
        try
        {
            //getValueTable(ControlId, drPageKey, DDL);
            getValueTable(ControlId, drPageKey, txtobj, hfobj,"");
            if (HttpContext.Current.Request.QueryString["MK"] != null)
                if (HttpContext.Current.Request.QueryString["MK"] != "")
                    //DDL.SelectedValue = HttpContext.Current.Request.QueryString["MK"];
                    //txtobj.Text = HttpContext.Current.Request.QueryString["MK"].ToString();
            //hfobj.Value = HttpContext.Current.Request.QueryString["MK"].ToString();


            /*if (HttpContext.Current.Session["PK"] != null)
                if (HttpContext.Current.Session["PK"].ToString() != "")
                {
                    DDL.SelectedValue = HttpContext.Current.Session["PK"].ToString();
                }*/
            if (HttpContext.Current.Request.QueryString["VMODE"] != null)
                if (HttpContext.Current.Request.QueryString["VMODE"] == "1")
                    //DDL.Enabled = false;
                    txtobj.Enabled = false;
        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("FillMasterValues : Error : " + ex.Message);
            return false;
        }
        return true;
    }

    private void getValueTable(string ControlId, DataRow drPageKey, TextBox txtobj,HiddenField hfobj,string masterdata)
    {
        string sSql = "select object_table from sy_obj where object_id='" + drPageKey["master_obj"].ToString() + "'";
        string sTableName = HDD.GetColumnValue(sSql).Replace("'", "''");
        sSql = "select * from [" + sTableName + "] ";
        security objSec = new security();
        string sRowLevel = objSec.getRowLevelSecurityData(sTableName, sTableName);
        if (sRowLevel != "")
            sSql = sSql + " where 1 = 1 and " + sRowLevel;
        sSql += " order by [" + sTableName + "].[" + drPageKey["display_att"].ToString().Replace("'", "''") + "]";
        DataTable dtValues = HDD.GetDataTable(sSql);

        if (dtValues != null)
        {
            if (dtValues.Rows.Count > 0)
            {
                if (HttpContext.Current.Request.QueryString["MK"] != null)
                {
                    DataRow[] drr = dtValues.Select("af_row_id='" + HttpContext.Current.Request.QueryString["MK"].ToString() + "'");
                    if (drr.Length > 0)
                    {
                        hfobj.Value = drr[0]["af_row_id"].ToString();
                        txtobj.Text = drr[0][drPageKey["display_att"].ToString()].ToString();
                    }
                }

            }
        }
        //DDL.DataSource = dtValues;
        //DDL.DataTextField = drPageKey["display_att"].ToString();
        //DDL.DataValueField = "af_row_id";
        //DDL.DataBind();

        /*foreach (DataRow dr in dtValues.Rows)
            DDL.Items.Add(new ListItem(dr[drPageKey["display_att"].ToString()].ToString(), dr["af_row_id"].ToString()));
        */
        dtValues.Dispose();
        dtValues = null;
    }
    
    /// <summary>
    /// This will return a DataTable with Master page's keyvalues.
    /// 
    /// </summary>
    /// <returns></returns>
    public DataTable GetMasterDataFromAfrowid(string tablename,string storagedata)
    {
        DataTable dt = null;
        HDDatabase hdd = new HDDatabase();
        try
        {
            string sql = "select * from " + tablename + " ";

            if (storagedata != "")
            {
                sql = sql + "where af_row_id = '" + storagedata.Replace("'", "''") + "'";
            }
            dt = hdd.GetDataTable(sql);

        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("Error : GetMasterData :" + ex.Message);
        }
        finally
        {
            hdd = null;
        }
        return dt;

    }



    public DataTable GetMasterDataFromValue(string tablename, string Displayvalue,string displayfield)
    {
        DataTable dt = null;
        HDDatabase hdd = new HDDatabase();
        try
        {
            string sql = "select * from " + tablename + " ";

            if (Displayvalue != "")
            {
                sql = sql + "where "+displayfield.Replace("'","''")+" = '" + Displayvalue.Replace("'","''") + "'";
            }
            dt = hdd.GetDataTable(sql);

        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("Error : GetMasterData :" + ex.Message);
        }
        finally
        {
            hdd = null;
        }
        return dt;

    }

}
